package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 上传声音并保存在本地
     */
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(@Validate MultipartFile file) {
        Map<String, String> uploadTrack = vodService.uploadTrack(file);
        return Result.ok(uploadTrack);
    }


    /**
     * 保存声音信息
     */
    @Operation(summary = "保存声音信息")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validate TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }

    @Operation(summary = "分页查询声音列表")
    @GuiGuCache(prefix = "fingUserTrackPage")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> fingUserTrackPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody TrackInfoQuery query) {
        Long userId = AuthContextHolder.getUserId();
        query.setUserId(userId);
        Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
        trackListVoPage = trackInfoService.findUserTrackPage(trackListVoPage, query);
        return Result.ok(trackListVoPage);
    }

    /**
     * 根据声音ID查询声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 修改声音信息
     *
     * @param id
     * @param trackInfoVo
     * @return
     */
    @PostMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }


    /**
     * 查询声音的列表
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "查询声音的列表")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
                                                             @PathVariable Long page,
                                                             @PathVariable Long limit) {
        Page<AlbumTrackListVo> trackListVoPage = new Page<>(page, limit);
        Long userId = AuthContextHolder.getUserId();
        trackListVoPage = trackInfoService.findAlbumTrackPage(trackListVoPage, albumId, userId);
        return Result.ok(trackListVoPage);
    }


    /**
     * @param trackId
     * @return
     */
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTracjStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    /**
     * @param trackId
     * @return
     */

    @GuiGuLogin
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(trackInfoService.findUserTrackPaidList(trackId, userId));
    }

    /**
     * 查询付费声音
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @Operation(summary = "查询付费声音")
    @GuiGuLogin
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,
                                                         @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(trackInfoService.findPaidTrackInfoList(userId, trackId, trackCount));
    }
}

